﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class MasterPage : System.Web.UI.MasterPage
{
    bool isLogin;

    public bool IsLogin
    {
        get { return isLogin; }
        set { isLogin = value; 
        switch(isLogin){
            case true:
                loginPanel.Visible = false;
                memberPanel.Visible = true;
                break;
            case false: 
                loginPanel.Visible = true;
                memberPanel.Visible = false;
                break;

        }
        
        }
    }
    
    string connString = "server=HOME; database=RealEstate; integrated security=true";
    string sql_selectLogin = "select * from Login where userID=@userID AND userPassword=@userPassword";
    protected void Page_Load(object sender, EventArgs e)
    {
        //loginPanel.Visible = false;
    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        if (Login(txtUser.Text.Trim(), txtPassword.Text.Trim()))
        {
            isLogin = true;
        }
        else
        {
            Response.Write("Wrong");
        }
    }
    #region 1st-functions
    bool Login(string _uID, string _uPass)
    {
        SqlConnection con;
        try
        {
            con = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(sql_selectLogin, con);
            cmd.Connection.Open();
            cmd.Parameters.Add("@userID", SqlDbType.Int);
            cmd.Parameters.Add("@userPassword", SqlDbType.VarChar);
            cmd.Parameters["@userID"].Value = _uID;
            cmd.Parameters["@userPassword"].Value = _uPass;

            SqlDataReader sr = cmd.ExecuteReader();
            if (sr.Read())
            {
                return true;
            }
            return false;
        }
        catch(Exception ex){
            Console.Write("Login function error: "+ex.Message);
            return false;
        }
    }
    #endregion
}
